//
//  form/Mesh.h
//  crag
//
//  Created by John on 10/24/09.
//  Copyright 2009, 2010 John McFarlane. All rights reserved.
//  This program is distributed under the terms of the GNU General Public License.
//

#pragma once

#include "defs.h"
#include "MeshProperties.h"

#include "gfx/Mesh.h"
#include "gfx/LitVertex.h"

// activates flat-shaded mesh style by adding exta vertices with common normals
#define CRAG_FORM_FLAT_SHADE

namespace form
{
	// forward-declaration
	class Point;
	
	// Stores a complete mesh of the formations.
	// Is generated by the NodeBuffer and then passed to a VertexBufferObject for rendering.
	class Mesh
	{
	public:
		// types
		typedef gfx::LitVertex Vertex;
#if defined(CRAG_FORM_FLAT_SHADE)
		typedef gfx::LitMesh::VertexArrayType LitMesh;
#else
		typedef gfx::LitMesh LitMesh;
#endif
		typedef Vertex::Color Color;
		
		// functions
		void Reserve(int max_num_verts, int max_num_tris);
		void Clear();
		void NormalizeNormals();
		
		MeshProperties & GetProperties();
		MeshProperties const & GetProperties() const;

#if ! defined(CRAG_FORM_FLAT_SHADE)
		Vertex & GetVertex(Point & point, Color color);
	private:
		Vertex & AddVertex(Point const & p, Color color);
		
		void AddFace(Vertex & a, Vertex & b, Vertex & c, Vertex::Vector3 const & normal);
	public:
		void AddFace(Point & a, Point & b, Point & c, Vertex::Vector3 const & normal, gfx::Color4b color);
#else
		void AddFace(Point const & a, Point const & b, Point const & c, Vertex::Vector3 const & normal, gfx::Color4b color);
#endif

		LitMesh const & GetLitMesh() const;
		
		CRAG_VERIFY_INVARIANTS_DECLARE(Mesh);
		
		// variables
	private:
		LitMesh _lit_mesh;
		MeshProperties properties;
	};
	
}
